package com.zw.aspect;

import com.zw.annotation.OwnAnnotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class TestAspect {

    @Pointcut("@annotation(com.zw.annotation.OwnAnnotation)" )
    public void addAdvice(){}

    @Around("addAdvice()")
    public Object Interceptor(ProceedingJoinPoint joinPoint){
        System.out.println("环绕增强--通知之开始");
        Object retmsg=null;
        try {
            retmsg = joinPoint.proceed(new Object[]{"04"});
        } catch (Throwable e) {
            e.printStackTrace();
        }
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            String deviceId = (String) args[0];
            /*if (!"03".equals(deviceId)) {
                return "no anthorization";
            }*/
        }
        System.out.println("环绕增强最终的结果为:"+retmsg);
        return retmsg;
    }
    @Before("addAdvice()")
    public void before(JoinPoint joinPoint){
        /*//测试JoinPoint 的各属性值含义
        //joinPoint.getArgs()
        Object[] args = joinPoint.getArgs();
        System.out.println("getArgs结果为:"+args[0]);
        //joinPoint.getSignature()
        System.out.println("getSignature结果为:"+joinPoint.getSignature().getModifiers());
        //joinPoint.getKind()
        System.out.println("getKind结果为:"+joinPoint.getKind());
        //joinPoint.getSourceLocation()
        System.out.println("getSourceLocation结果为:"+joinPoint.getSourceLocation());
        //joinPoint.getStaticPart()
        System.out.println("getStaticPart结果为:"+joinPoint.getStaticPart().getId());
        //joinPoint.getTarget()
        System.out.println("getTarget结果为:"+joinPoint.getTarget().toString());
        //joinPoint.getThis()
        System.out.println("getThis结果为:"+joinPoint.getThis().toString());*/
        MethodSignature sign =  (MethodSignature)joinPoint.getSignature();
        Method method = sign.getMethod();
        OwnAnnotation annotation = method.getAnnotation(OwnAnnotation.class);
        System.out.println("前置增强--annotation的结果为:"+annotation);
    }

    @After("addAdvice()")
    public void after() {
        System.out.println("后置增强执行");
    }
}
